﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ThaireInvestmentWeb.Util;
using ThaireInvestmentWeb.WcfSerivce;

namespace ThaireInvestmentWeb.Models
{
    public class ManageDepositModel
    {
        public SearchDepositFilter SearchFilter { get; set; }
        public ManageDepositModel()
        {
            Init();
        }
        void Init()
        {
            CompanyList = new List<SelectListItem> { WebUtil.SelectListSelectDefault() };
            FinanceTypeList = new List<SelectListItem> { WebUtil.SelectListSelectDefault() };
            DepositTypeList = new List<SelectListItem> { WebUtil.SelectListSelectDefault() };
            BankList = new List<SelectListItem> { WebUtil.SelectListSelectDefault() };

            if (SearchFilter == null)
                SearchFilter = new SearchDepositFilter();

            if (_data != null)
            {
                foreach (var item in _data.Companies)
                {
                    CompanyList.Add(new SelectListItem() { Text = item.CompanyNameTh, Value = item.CompanyId.ToString() });
                }
                foreach (var item in _data.FinanceTypes)
                {
                    FinanceTypeList.Add(new SelectListItem() { Text = item.ValueEN, Value = item.ID.ToString() });
                }
                foreach (var item in _data.DepositTypes)
                {
                    DepositTypeList.Add(new SelectListItem() { Text = item.AssetClassEN, Value = item.AssetClassID.ToString() });
                }
                foreach (var item in _data.Banks)
                {
                    BankList.Add(new SelectListItem() { Text = item.BankNameTh, Value = item.BankNameTh });
                }
            }
        }
        public void ClearFilterAndData()
        {
            Init();
        }

        static DepositHeaderScreenData _data;

        public static ManageDepositModel BuildModel(DepositHeaderScreenData data)
        {
            _data = data;
            return new ManageDepositModel();
        }

        [Required(ErrorMessage = "โปรดระบุ")]
        public int? SelectedCompany
        {
            get { return SearchFilter.Company; }
            set { SearchFilter.Company = value; }
        }
        public IList<SelectListItem> CompanyList { get; set; }

        public int? SelectedDepositType
        {
            get { return SearchFilter.DepositType; }
            set { SearchFilter.DepositType = value; }
        }
        public IList<SelectListItem> DepositTypeList { get; set; }

        public int? SelectedFinanceType
        {
            get { return SearchFilter.FinanceType; }
            set { SearchFilter.FinanceType = value; }
        }
        public IList<SelectListItem> FinanceTypeList { get; set; }

        public string SelectedBank
        {
            get { return SearchFilter.BankName; }
            set { SearchFilter.BankName = value; }
        }
        public IList<SelectListItem> BankList { get; set; }


        /// <summary>
        /// AccountVoucherNO
        /// </summary>
        public string AccountVoucherNO
        {
            get { return SearchFilter.Number; }
            set { SearchFilter.Number = value; }
        }


        public IList<DepositHeaderDTO> Result
        {
            set
            {
                if (value != null)
                {
                    if (HeaderAdaps == null)
                        HeaderAdaps = new List<DepositHeaderAdapter>();
                    HeaderAdaps.Clear();
                    foreach (var dto in value)
                    {
                        HeaderAdaps.Add(new DepositHeaderAdapter(dto));
                    }
                }
            }
        }

        public IList<DepositHeaderAdapter> HeaderAdaps { get; set; }

        public String SearchBtn { get; set; }
        public String ResetBtn { get; set; }
    }

    public class DepositHeaderAdapter
    {
        private DepositHeaderDTO _dto;

        public DepositHeaderAdapter(DepositHeaderDTO dto)
        {
            _dto = dto;
        }

        public string Company { get { return _dto.Header.DepositCompanyName; } }
        public string DepositType { get { return _dto.DepositType; } }
        public string FinanceInstrumentType { get { return _dto.FinanceInstrument; } }
        public string BankName { get { return _dto.Header.BankName; } }
        public string AccountVoucherNO { get { return _dto.Header.AccountVoucherNO; } }
        public int DepositHeaderID { get { return _dto.Header.DepositHeaderID; } }
    }
}